﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public interface IChallengeSystem : ISystem
    {
        Dictionary<string, ChallengeSeries> ChallengeDic { get; }
    }

    public class ChallengeSystem : AbstractSystem, IChallengeSystem
    {
        public Dictionary<string, ChallengeSeries> ChallengeDic { get; private set; }

        protected override void OnInit()
        {
            ChallengeDic = new Dictionary<string, ChallengeSeries>();

            var saveUtility = this.GetUtility<ISaveUtility>();

            InitData(ChallengeDic);

            IUnRegister action = null;
            action = ActionKit.OnUpdate.Register(() =>
            {
                CheckFinished();
            });

            void CheckFinished()
            {
                var count = ChallengeDic.Values.Count(data => data.FinishedCondition(data));
                if (count == ChallengeDic.Values.Count)
                {
                    Debug.Log("游戏通关");
                    UIGameController.Default.GamePassPanel.Show();
                    action.UnRegister();
                }
            }

            foreach (var keyValuePair in ChallengeDic)
            {
                var saveChallengeData = saveUtility.LoadString(keyValuePair.Value.Key, string.Empty);

                Debug.Log(saveChallengeData);

                if (string.IsNullOrEmpty(saveChallengeData))
                {
                }
                else
                {
                    var ChallengeData =
                        JsonUtility.FromJson<SaveChallengeData>(saveChallengeData);

                    if (ChallengeData.Finished)
                    {
                        keyValuePair.Value.WithOnUnLock(() => true);
                        keyValuePair.Value.WithFinishedCondition((self) => true);
                        foreach (var valueChallengeData in keyValuePair.Value.ChallengeDatas)
                        {
                            valueChallengeData.WithFinished(true);
                            valueChallengeData.WithUnlock((self) => true);
                        }
                    }
                    else
                    {
                        for (var i = 0; i < ChallengeData.Challenges.Count; i++)
                        {
                            var data = keyValuePair.Value.ChallengeDatas[i];

                            if (ChallengeData.Challenges[i].Finished)
                            {
                                data.WithFinished(true);
                                data.WithUnlock((self) => true);
                                data.FinishedDay = ChallengeData.Challenges[i].FinishedDay;
                            }
                        }
                    }
                }
            }

            saveUtility.AddSaveDataAction(utility =>
            {
                foreach (var keyValuePair in ChallengeDic)
                {
                    var saveChallengeData = new SaveChallengeData();
                    saveChallengeData.Key = keyValuePair.Value.Key;
                    saveChallengeData.UnLock = keyValuePair.Value.OnUnLock();
                    saveChallengeData.Finished = keyValuePair.Value.FinishedCondition(keyValuePair.Value);

                    foreach (var ChallengeData in keyValuePair.Value.ChallengeDatas)
                    {
                        saveChallengeData.Challenges.Add(new SaveChallengeDataConfig()
                        {
                            Finished = ChallengeData.Finished,
                            FinishedDay = ChallengeData.Finished ? ChallengeData.FinishedDay : 0,
                        });
                    }

                    utility.SaveString(saveChallengeData.Key, JsonUtility.ToJson(saveChallengeData));
                }
            });
        }

        /// <summary>
        /// 初始化挑战数据
        /// </summary>
        /// <param name="challengeSeries"></param>
        void InitData(Dictionary<string, ChallengeSeries> challengeSeries)
        {
            var firstSeries = new ChallengeSeries()
                .WithKey("主线:我与未曾见过的她")
                .WithOnUnLock(() => true)
                .WithFinishedCondition(series =>
                {
                    var count = series.ChallengeDatas.Count(data => !data.Finished);
                    if (count == 0)
                    {
                        return true;
                    }

                    return false;
                });

            var secondSeries = new ChallengeSeries()
                .WithKey("支线:帮助村口家的老爷爷")
                .WithOnUnLock(() => true)
                .WithFinishedCondition(series =>
                {
                    var count = series.ChallengeDatas.Count(data => !data.Finished);
                    if (count == 0)
                    {
                        return true;
                    }

                    return false;
                });

            var thirdSeries = new ChallengeSeries()
                .WithKey("支线:路边摊的王大妈")
                .WithOnUnLock(() =>
                {
                    if (secondSeries.FinishedCondition(secondSeries))
                    {
                        return true;
                    }

                    return false;
                })
                .WithFinishedCondition(series =>
                {
                    var count = series.ChallengeDatas.Count(data => !data.Finished);
                    if (count == 0)
                    {
                        return true;
                    }

                    return false;
                });

            AddFirstSeries.AddChallenge(firstSeries);
            AddSecondSeries.AddChallenge(secondSeries);
            AddThirdSeries.AddChallenge(thirdSeries);

            challengeSeries.Add(firstSeries.Key, firstSeries);
            challengeSeries.Add(secondSeries.Key, secondSeries);
            challengeSeries.Add(thirdSeries.Key, thirdSeries);
        }
    }
}